package com.inei.eea2014.model.dao;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.inei.eea2014.model.connection.UtilDB;
import com.inei.eea2014.model.entity.Entrevista;

public class EntrevistaDao implements ICRUD<Entrevista> {

	// variables
	private UtilDB util;
	private SQLiteDatabase db;
	private Cursor cursor = null;

	// constructor
	public EntrevistaDao(Context context) {
		util = new UtilDB(context);
	}

	// metodos ICRUD
	@Override
	public Entrevista buscar(Entrevista o) {
		Entrevista obj = null;
		db = util.getReadableDatabase();

		cursor = db
				.rawQuery(
						"select * from Entrevista where NRO_ENT=? and RUC=? and USUARIO=?",
						new String[] { o.getNRO_ENT() + "", o.getRUC() + "",
								o.getUSUARIO() + "" });
		if (cursor.moveToFirst()) {
			obj = new Entrevista();
			obj.setNRO_ENT(cursor.getInt(cursor.getColumnIndex("NRO_ENT")));
			obj.setRUC(cursor.getString(cursor.getColumnIndex("RUC")));
			obj.setUSUARIO(cursor.getString(cursor.getColumnIndex("USUARIO")));
			obj.setC0024_000_01_T(cursor.getString(cursor
					.getColumnIndex("C0024_000_01_T")));
			obj.setC0024_000_02_T(cursor.getString(cursor
					.getColumnIndex("C0024_000_02_T")));
			obj.setC0024_000_03_T(cursor.getString(cursor
					.getColumnIndex("C0024_000_03_T")));
			obj.setC0024_000_04_T(cursor.getString(cursor
					.getColumnIndex("C0024_000_04_T")));
			obj.setC0024_000_05_T(cursor.getString(cursor
					.getColumnIndex("C0024_000_05_T")));
			obj.setC0024_000_06_T(cursor.getString(cursor
					.getColumnIndex("C0024_000_06_T")));
			obj.setGPS_LAT(cursor.getString(cursor.getColumnIndex("GPS_LAT")));
			obj.setGPS_LON(cursor.getString(cursor.getColumnIndex("GPS_LON")));
		}

		Cerrar();
		return obj;
	}

	@Override
	public List<Entrevista> leer() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int crear(Entrevista o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();

		ContentValues valores = new ContentValues();
		valores.put("NRO_ENT", o.getNRO_ENT());
		valores.put("RUC", o.getRUC());
		valores.put("USUARIO", o.getUSUARIO());
		valores.put("C0024_000_01_T", o.getC0024_000_01_T());
		valores.put("C0024_000_02_T", o.getC0024_000_02_T());
		valores.put("C0024_000_03_T", o.getC0024_000_03_T());
		valores.put("C0024_000_04_T", o.getC0024_000_04_T());
		valores.put("C0024_000_05_T", o.getC0024_000_05_T());
		valores.put("C0024_000_06_T", o.getC0024_000_06_T());
		valores.put("GPS_LAT", o.getGPS_LAT());
		valores.put("GPS_LON", o.getGPS_LON());

		resultado = (int) db.insert("Entrevista", null, valores);
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}

		valores.clear();
		Cerrar();
		return resultado;
	}

	@Override
	public int actualizar(Entrevista o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();

		ContentValues valores = new ContentValues();
		valores.put("C0024_000_01_T", o.getC0024_000_01_T());
		valores.put("C0024_000_02_T", o.getC0024_000_02_T());
		valores.put("C0024_000_03_T", o.getC0024_000_03_T());
		valores.put("C0024_000_04_T", o.getC0024_000_04_T());
		valores.put("C0024_000_05_T", o.getC0024_000_05_T());
		valores.put("C0024_000_06_T", o.getC0024_000_06_T());
		valores.put("GPS_LAT", o.getGPS_LAT());
		valores.put("GPS_LON", o.getGPS_LON());

		resultado = (int) db.update(
				"Entrevista",
				valores,
				"NRO_ENT=? and RUC=? and USUARIO=?",
				new String[] { o.getNRO_ENT() + "", o.getRUC() + "",
						o.getUSUARIO() + "" });
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}

		valores.clear();
		Cerrar();
		return resultado;
	}

	@Override
	public int eliminar(Entrevista o) {
		// TODO Auto-generated method stub
		return 0;
	}

	// otros metodos
	public int total(Entrevista o) {
		int tot = 0;
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from Entrevista where RUC=? and USUARIO=?",
				new String[] { o.getRUC() + "", o.getUSUARIO() + "" });
		if (cursor.moveToFirst()) {
			tot = cursor.getCount();
		}
		Cerrar();
		return tot;
	}

	public List<Entrevista> leer(Entrevista o) {
		List<Entrevista> lista = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery(
				"select * from Entrevista where RUC=? and USUARIO=?",
				new String[] { o.getRUC() + "", o.getUSUARIO() + "" });
		if (cursor.moveToFirst()) {
			lista = new ArrayList<Entrevista>();
			while (!cursor.isAfterLast()) {
				Entrevista obj = new Entrevista();
				obj.setNRO_ENT(cursor.getInt(cursor.getColumnIndex("NRO_ENT")));
				obj.setRUC(cursor.getString(cursor.getColumnIndex("RUC")));
				obj.setUSUARIO(cursor.getString(cursor
						.getColumnIndex("USUARIO")));
				obj.setC0024_000_01_T(cursor.getString(cursor
						.getColumnIndex("C0024_000_01_T")));
				obj.setC0024_000_02_T(cursor.getString(cursor
						.getColumnIndex("C0024_000_02_T")));
				obj.setC0024_000_03_T(cursor.getString(cursor
						.getColumnIndex("C0024_000_03_T")));
				obj.setC0024_000_04_T(cursor.getString(cursor
						.getColumnIndex("C0024_000_04_T")));
				obj.setC0024_000_05_T(cursor.getString(cursor
						.getColumnIndex("C0024_000_05_T")));
				obj.setC0024_000_06_T(cursor.getString(cursor
						.getColumnIndex("C0024_000_06_T")));
				obj.setGPS_LAT(cursor.getString(cursor
						.getColumnIndex("GPS_LAT")));
				obj.setGPS_LON(cursor.getString(cursor
						.getColumnIndex("GPS_LON")));
				lista.add(obj);
				cursor.moveToNext();
			}
		}
		Cerrar();
		return lista;
	}

	private void Cerrar() {
		if (cursor != null) {
			cursor.close();
		}
		if (db.inTransaction()) {
			db.endTransaction();
		}
		if (db.isOpen()) {
			db.close();
		}
	}
}
